import android.content.Context
import android.content.SharedPreferences

class SharedPreferencesManager(context: Context) {
    private val sharedPreferences: SharedPreferences = context.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE)

    fun saveAuthToken(token: String) {
        sharedPreferences.edit().putString("auth_token", token).apply()
    }

    fun getAuthToken(): String? {
        return sharedPreferences.getString("auth_token", null)
    }

    fun clearAuthToken() {
        sharedPreferences.edit().remove("auth_token").apply()
    }

    fun saveDiaryEntry(date: String, title: String) {
        val editor = sharedPreferences.edit()
        editor.putString("diary_entry_date", title)
        editor.apply()
    }

    fun getDiaryEntry(date: String): String? {
        return sharedPreferences.getString("diary_entry_date", null)
    }

    fun removeDiaryEntry(date: String) {
        sharedPreferences.edit().remove("diary_entry_date").apply()
    }

    fun clearAllDiaryEntries() {
        sharedPreferences.edit().let { editor ->
            sharedPreferences.all.keys.filter { it.startsWith("diary_entry_") }.forEach {
                editor.remove(it)
            }
            editor.apply()
        }
    }

    fun getAllDiaryEntries(): Map<String, *> {
        return sharedPreferences.all.filterKeys { it.startsWith("diary_entry_") }
    }

    fun saveLatestWorkoutPlanId(planId: Int) {
        sharedPreferences.edit().putInt("latest_workout_plan_id", planId).apply()
    }

    fun getLatestWorkoutPlanId(): Int {
        return sharedPreferences.getInt("latest_workout_plan_id", -1)
    }
}